home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / circuits / irsim_ta.z / irsim_ta / irsim / src / ana11 / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-08  |  9.7 KB  |  509 lines

  1. /*
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     *********************************************************************
  13.  */
  14.  
  15. /*
  16.  * Logic analyzer event manager.
  17.  */
  18.  
  19. #include <signal.h>
  20. #include <fcntl.h>
  21.  
  22. #ifdef SYS_V
  23. #    include <termio.h>
  24. #    ifdef hpux
  25. #    define    SigBlock( sig )    sigsetmask( sigmask( sig ) )
  26. #    define    SigRelease( sig )    \
  27.             sigsetmask( sigsetmask( 0 ) & ~sigmask( sig ) )
  28. #    else
  29. #    include <stropts.h>
  30. #    define    signal( A, B )        sigset( A, B )
  31. #    define    SigBlock( sig )        sighold( sig )
  32. #    define    SigRelease( sig )    sigrelse( sig )
  33. #    endif
  34. #    define    vfork()        fork()
  35. #else
  36. #    include <sys/ioctl.h>
  37. #    ifndef    sigmask
  38. #    define sigmask( m )        ( 1 << (( m ) - 1 ) )
  39. #    endif
  40. #    define    SigBlock( sig )    sigsetmask( sigmask( sig ) )
  41. #    define    SigRelease( sig )    \
  42.             sigsetmask( sigsetmask( 0 ) & ~sigmask( sig ) )
  43. #endif SYS_V
  44.  
  45. #include <stdio.h>
  46. #include "ana.h"
  47. #include "X11/Xutil.h"
  48. #include "ana_glob.h"
  49. #include "graphics.h"
  50. #include "helper.h"
  51.  
  52.  
  53. #define    WITHINY( Y, Box )    ( (Y <= (Box).bot) and (Y >= (Box).top) )
  54. #define    WITHINX( X, Box )    ( (X >= (Box).left) and (X <= (Box).right) )
  55.  
  56.  
  57. private    int     x_server = 0;
  58. private    Func    FGetEvent = NULL;
  59. private    int     x_helper = 0;        /* process id of helper process */
  60.  
  61.  
  62. private void WindowResize( ev )
  63.   XConfigureEvent  *ev;
  64.   {
  65.     int  ret;
  66.  
  67.     if( ev->width != XWINDOWSIZE or ev->height != YWINDOWSIZE )
  68.       {
  69.     XWINDOWSIZE = ev->width;
  70.     YWINDOWSIZE = ev->height;
  71.     ret = WindowChanges();
  72.       }
  73.   }
  74.  
  75.  
  76. private void WindowExposed( event )
  77.   XExposeEvent  *event;
  78.   {
  79.     BBox  box;
  80.  
  81.     box.left = event->x;
  82.     box.right = event->x + event->width - 1;
  83.     box.top = event->y;
  84.     box.bot = event->y + event->height - 1;
  85.     RedrawWindow( box );
  86.   }
  87.  
  88.  
  89. private void HandleButton( ev )
  90.   XButtonEvent  *ev;
  91.   {
  92.     if( WITHINY( ev->y, scrollBox ) )
  93.     DoScrollBar( ev );
  94.     else if( WITHINY( ev->y, traceBox) )
  95.       {
  96.     if( WITHINX( ev->x, namesBox ) )
  97.         MoveTrace( ev->y );
  98.     else if( WITHINX( ev->x, traceBox ) )
  99.         DoCursor( ev );
  100.     else if( WITHINX( ev->x, cursorBox ) )
  101.         SelectCursTrace( ev->y );
  102.       }
  103.     else if( WITHINY( ev->y, bannerBox ) )
  104.       {
  105.     if( WITHINX( ev->x, iconBox ) )
  106.         IconifyMe();
  107.     else if( WITHINX( ev->x, sizeBox ) )
  108.         ResizeMe();
  109.     else if( WITHINX( ev->x, menuBox ) )
  110.         DoMenu( ev->x, ev->y );
  111.     else
  112.       {
  113.         switch( ev->button & (Button1 | Button2 | Button3) )
  114.           {
  115.         case Button1 : XRaiseWindow( display, window ); break;
  116.         case Button2 : MoveMe( ev->x, ev->y ); break;
  117.         case Button3 : XLowerWindow( display, window ); break;
  118.           }
  119.       }
  120.       }
  121.   }
  122.  
  123.  
  124. private void HandleKey( ev )
  125.   XKeyEvent  *ev;
  126.   {
  127.     char  buff[ 40 ];
  128.     int   nChars, i;
  129.  
  130.     nChars = XLookupString( ev, buff, sizeof( buff ), NULL, NULL );
  131.     for( i = 0; i < nChars; i++ )
  132.       {
  133.     switch( buff[i] )
  134.       {
  135.         case 'i' :
  136.         case 'o' :
  137.         Zoom( buff );
  138.         break;
  139.  
  140.         case 'd' :
  141.         DeltaT( buff );
  142.         break;
  143.  
  144.         case 'm' :
  145.         MoveToTime( buff );
  146.         break;
  147.  
  148.         case 'p' :
  149.         printPS( buff );
  150.         break;
  151.  
  152.         case 'w' :
  153.         SetWidth( buff );
  154.         break;
  155.  
  156.         default:
  157.         XBell( display, 0 );
  158.       }
  159.       }
  160.   }
  161.  
  162.  
  163. public void SendEventTo( f )
  164.   Func  f;
  165.   {
  166.     FGetEvent = f;
  167.   }
  168.  
  169.  
  170.  
  171. private void EventHandler()
  172.   {
  173.     XEvent  event;
  174.  
  175.     if( XEventsQueued( display, QueuedAfterReading ) > 0 )
  176.       {
  177.     do
  178.       {
  179.         XNextEvent( display, &event );
  180.         switch( event.type )
  181.           {
  182.         case ButtonPress :
  183.             if( windowState.tooSmall )
  184.             ;
  185.             else if( FGetEvent != NULL )
  186.             (*FGetEvent)( &event );
  187.             else
  188.             HandleButton( &event.xbutton );
  189.             break;
  190.  
  191.         case KeyPress :
  192.             if( windowState.tooSmall )
  193.             ;
  194.             else if( FGetEvent != NULL )
  195.             (*FGetEvent)( &event.xkey );
  196.             else
  197.             HandleKey( &event.xkey );
  198.              break;
  199.  
  200.         case EnterNotify :
  201.             if( event.xcrossing.window == window )
  202.             WindowCrossed( TRUE );
  203.             break;
  204.  
  205.         case LeaveNotify :
  206.             if( event.xcrossing.window == window )
  207.             WindowCrossed( FALSE );
  208.         break;
  209.  
  210.         case Expose :
  211.             if( event.xexpose.window == iconW )
  212.             RedrawIcon( &event.xexpose );
  213.             else if( windowState.tooSmall )
  214.             RedrawSmallW();
  215.             else if( event.xexpose.window == window )
  216.             WindowExposed( &event.xexpose );
  217.             break;
  218.  
  219.         case ConfigureNotify :
  220.             WindowResize( &event.xconfigure );
  221.             break;
  222.  
  223.         case UnmapNotify :
  224.             windowState.iconified = TRUE;
  225.             break;
  226.  
  227.         case MapNotify :
  228.             windowState.iconified = FALSE;
  229.             windowState.selected = FALSE;
  230.             (void) WindowChanges();
  231.             break;
  232.  
  233.         default : ;
  234.           }
  235.       }
  236.     while( XPending( display ) );
  237.       }
  238.  
  239. #ifdef NEED_HELPER
  240.     kill( x_helper, SIGINT );
  241. #endif
  242.  
  243. #ifdef SYS_V
  244.     #ifdef NO_SIGIO
  245.         signal(SIGUSR2, EventHandler);
  246.     #else
  247.         signal( SIGIO, EventHandler );
  248.     #endif
  249. #endif
  250.  
  251.    }
  252.  
  253.  
  254. private void DisabledEventHandler()
  255.   {
  256.     XEvent  event;
  257.  
  258.     if( XEventsQueued( display, QueuedAfterReading ) > 0 )
  259.       {
  260.     do
  261.       {
  262.         XNextEvent( display, &event );
  263.         switch( event.type )
  264.           {
  265.         case EnterNotify :
  266.             if( event.xcrossing.window == window )
  267.             WindowCrossed( TRUE );
  268.             break;
  269.  
  270.         case LeaveNotify :
  271.             if( event.xcrossing.window == window )
  272.             WindowCrossed( FALSE );
  273.             break;
  274.  
  275.         case Expose :
  276.             if( event.xexpose.window == iconW )
  277.             RedrawIcon( &event );
  278.             else if( windowState.tooSmall )
  279.             RedrawSmallW();
  280.             break;
  281.  
  282.         case ConfigureNotify :
  283.             WindowResize( &event.xconfigure );
  284.             break;
  285.  
  286.         case UnmapNotify :
  287.             windowState.iconified = TRUE;
  288.             break;
  289.  
  290.         case MapNotify :
  291.             windowState.iconified = FALSE;
  292.             windowState.selected = FALSE;
  293.             break;
  294.  
  295.         default : ;
  296.           }
  297.       }
  298.     while( XPending( display ) );
  299.       }
  300.  
  301. #ifdef NEED_HELPER
  302.     kill( x_helper, SIGINT );
  303. #endif
  304.  
  305. #ifdef SYS_V
  306.     #ifdef NO_SIGIO
  307.         signal(SIGUSR2, DisabledEventHandler);
  308.     #else
  309.         signal( SIGIO, DisabledEventHandler );
  310.     #endif
  311. #endif
  312.  
  313.    }
  314.  
  315.  
  316. #ifdef NEED_HELPER
  317.  
  318. private int StartHelper( fd )
  319.   int  fd;
  320.   {
  321.     extern char  *cad_bin;
  322.     static char  helper_name[] = "anXhelper";
  323.     char         helper_proc[ 256 ];
  324.  
  325.     sprintf( helper_proc, "%s/%s", cad_bin, helper_name );
  326.  
  327.     if( x_helper == 0 )            /* never start 2 processes */
  328.     x_helper = vfork();
  329.  
  330.     if( x_helper == -1 )
  331.       {
  332.     fprintf( stderr, "can't fork helper process\n" );
  333.     x_helper = 0;
  334.     return( FALSE );
  335.       }
  336.     else if( x_helper == 0 )        /* child process */
  337.       {
  338.     if( dup2( fd, 0 ) == -1 )
  339.       {
  340.         fprintf( stderr, "can't dup descriptor\n" );
  341.         _exit( 1 );
  342.       }
  343.     execl( helper_proc, helper_name, NULL );
  344.     fprintf( stderr, "can't exec helper process: %s\n", helper_name );
  345.     _exit( 1 );
  346.       }
  347.     sleep( 1 );                /* make sure process started */
  348.  
  349.     if( kill( x_helper, SIGUSR1 ) != 0 )
  350.       {
  351.     x_helper = 0;
  352.     fprintf( stderr, "helper process is dead\n" );
  353.     return( FALSE );
  354.       }
  355.     return( TRUE );
  356.   }
  357.  
  358. #endif NEED_HELPER
  359.  
  360.  
  361.  
  362. public int InitHandler( fd )
  363.   int fd;
  364.   {
  365.     int  flags;
  366.  
  367.     x_server = fd;
  368.     #ifdef NO_SIGIO
  369.         signal(SIGUSR2, EventHandler);
  370.     #else
  371.         signal( SIGIO, EventHandler );
  372.     #endif
  373.     
  374. #ifdef NEED_HELPER
  375.  
  376.     return( StartHelper( fd ) );
  377.  
  378. #else
  379.  
  380. # ifdef SYS_V
  381.     flags = 1;
  382.     if( ioctl( fd, FIOASYNC, &flags ) == -1 )
  383.       {
  384.     fprintf( stderr, "ioctl: can not set FIOASYNC\n" );
  385.     return( FALSE );
  386.       }
  387. # else
  388.     if( (flags = fcntl( fd, F_GETFL, 0 )) == -1 )
  389.       {
  390.     fprintf( stderr, "fctl: can not do F_GETFL\n" );
  391.     return( FALSE );
  392.       }
  393.  
  394.     if( (flags & FASYNC) == 0 )
  395.       {
  396.     if( fcntl( fd, F_SETFL, flags | FASYNC ) == -1 )
  397.       {
  398.         fprintf( stderr, "fctl: can not do F_SETFL\n" );
  399.         return( FALSE );
  400.       }
  401.       }
  402.  
  403. # endif SYS_V
  404.  
  405. # ifdef F_SETOWN
  406.     if( fcntl( fd, F_SETOWN, getpid() ) == -1 )
  407.       {
  408.     fprintf( stderr, "fctl: can not do F_SETOWN\n" );
  409.     return( FALSE );
  410.       }
  411. # endif
  412.  
  413.     return( TRUE );
  414.  
  415. #endif NEED_HELPER
  416.   }
  417.  
  418.  
  419. public void DisableInput()
  420.   {
  421.       #ifdef NO_SIGIO
  422.           SigBlock(SIGUSR2);
  423.       #else
  424.         SigBlock( SIGIO );
  425.     #endif
  426.     
  427.   }
  428.  
  429.  
  430. public void EnableInput()
  431.   {
  432.     if( XPending( display ) )
  433.       {
  434.     EventHandler();
  435.       }
  436.     else
  437.       {
  438. #ifdef NEED_HELPER
  439.     kill( x_helper, SIGINT );
  440. #endif
  441.     ;
  442.       }
  443.     #ifdef NO_SIGIO
  444.         SigRelease(SIGUSR2);
  445.     #else
  446.         SigRelease( SIGIO );
  447.     #endif
  448.   }
  449.  
  450.  
  451. public void DisableAnalyzer()
  452.   {
  453.       #ifdef NO_SIGIO
  454.           SigBlock(SIGUSR2);
  455.           signal( SIGUSR2, DisabledEventHandler );
  456.         SigRelease( SIGUSR2 );
  457.       #else
  458.         SigBlock( SIGIO );
  459.         signal( SIGIO, DisabledEventHandler );
  460.         SigRelease( SIGIO );
  461.     #endif
  462.     
  463.  
  464.   }
  465.  
  466.  
  467. public void EnableAnalyzer()
  468.   {
  469.     int  change;
  470.  
  471.     DisableInput();
  472.  
  473.     updatePending = FALSE;
  474.  
  475.     if( FGetEvent != NULL )
  476.       {
  477.     (*FGetEvent)( (XEvent *) NULL );    /* let handler know */
  478.     FGetEvent = NULL;
  479.       }
  480.  
  481.     #ifdef NO_SIGIO
  482.         signal(SIGUSR2, EventHandler);
  483.     #else
  484.         signal( SIGIO, EventHandler );
  485.     #endif
  486.  
  487.     if( windowState.iconified == 0 and windowState.tooSmall == 0 )
  488.       {
  489.     change = WindowChanges();
  490.     if( not (change & RESIZED) )
  491.       {
  492.         BBox  box;
  493.  
  494.         box.left = box.top = 0;
  495.         box.right = XWINDOWSIZE - 1;
  496.         box.bot = YWINDOWSIZE - 1;
  497.         RedrawWindow( box );
  498.       }
  499.       }
  500.     EnableInput();
  501.   }
  502.  
  503.  
  504. public void TerminateAnalyzer()
  505.   {
  506.     if( x_helper != 0 )
  507.     kill( x_helper, SIGKILL );
  508.   }
  509.